package mianyang.zhangsan.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.mapper.AttendanceMapper;
import mianyang.zhangsan.com.pojo.Attendance;
import mianyang.zhangsan.com.pojo.AttendanceCustom;
import mianyang.zhangsan.com.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("attendanceService")
public class AttendanceServiceImpl implements AttendanceService {
    @Autowired
    private AttendanceMapper attendanceMapper;

    /**
     * 根据条件分页查询
     * @param page
     * @param size
     * @param studentId
     * @param courseId
     * @param type
     * @param date
     * @return
     */
    @Override
    public Page<Attendance> queryByPage(int page, int size, int studentId, int courseId, String type, String date) {
        QueryWrapper<Attendance> wrapper = new QueryWrapper<>();
        if (studentId != 0){
            wrapper.eq("student_id", studentId);
        }
        if (courseId != 0){
            wrapper.eq("course_id", courseId);
        }
        if (!"".equals(type)){
            wrapper.eq("type", type);
        }
        if (!"".equals(date)){
            wrapper.eq("date", date);
        }
        if (studentId == 0 && courseId == 0 && type == null && date == null){
            return attendanceMapper.selectPage(new Page<>(page, size), null);
        }
        return attendanceMapper.selectPage(new Page<>(page, size), wrapper);
    }

    /**
     * 添加考勤信息
     * @param attendance
     */
    @Override
    public void addAttendance(Attendance attendance) {
        attendanceMapper.insert(attendance);
    }

    /**
     * 根据id删除考勤信息
     * @param id
     */
    @Override
    public void deleteAttendance(int id) {
        QueryWrapper<Attendance> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        attendanceMapper.delete(wrapper);
    }

    @Override
    public AttendanceCustom attendancePackage(Attendance attendance, String courseName, String studentName) {
        AttendanceCustom custom = new AttendanceCustom();
        custom.setId(attendance.getId());
        custom.setCourseName(courseName);
        custom.setStudentName(studentName);
        custom.setType(attendance.getType());
        custom.setDate(attendance.getDate());
        return custom;
    }
}
